/******************************************************************************
 *  Copyright 2013 Bernhard Grünewaldt                                        *
 *                                                                            *
 *  Licensed under the Apache License, Version 2.0 (the "License");           *
 *  you may not use this file except in compliance with the License.          *
 *  You may obtain a copy of the License at                                   *
 *                                                                            *
 *      http://www.apache.org/licenses/LICENSE-2.0                            *
 *                                                                            *
 *  Unless required by applicable law or agreed to in writing, software       *
 *  distributed under the License is distributed on an "AS IS" BASIS,         *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
 *  See the License for the specific language governing permissions and       *
 *  limitations under the License.                                            *
 ******************************************************************************/
package ut.com.google.code.greenwood.confluencemavenreleaseinfomacro.macro;

import java.io.StringWriter;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

public class MockHelper {
	
	/**
	 * what normally confluence would do, do it with plain velocity
	 * 
	 * @param str
	 * @param ctx
	 * @return
	 */
    public String velocityUtils_getRenderedTemplate(String templateName, Map<String, Object> plainContext) {
			    StringWriter sw = new StringWriter();
		
			    try {	      
			    	VelocityEngine ve = new VelocityEngine();
			    	ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
			    	ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
			    	ve.init();
				    Velocity.init();
				    VelocityContext context = new VelocityContext();
				    if (plainContext != null) {
				    	for (Entry<String, Object> entry: plainContext.entrySet()) {
				    		context.put(entry.getKey(), entry.getValue());
				    	}
				    }		    
			    	Template template = ve.getTemplate(templateName);   	
				    template.merge( context, sw );
			    } catch( ResourceNotFoundException rnfe ) {
			       System.err.println("toHtml(): couldn't find the template.");
			       rnfe.printStackTrace();
			    } catch( ParseErrorException pee ) {
			    	System.err.println("toHtml(): syntax error: problem parsing the template.");
			    	pee.printStackTrace();
			    } catch( MethodInvocationException mie )  {
			    	System.err.println("toHtml(): something invoked in the template threw an exception.");
			    	mie.printStackTrace();
			    } catch( Exception e ) {
			    	System.err.println("toHtml(): Generic Exception."+e.getMessage());
			    	e.printStackTrace();
			    }
			    return sw.toString();
	}
}
